package com.atguigu.lease.common.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
//3、当前对象被容器初始化之前 先初始化minioConfig对象
@DependsOn(value = "minioConfig")
//@EnableConfigurationProperties(value = {MinioConfig.class})
public class Knife4jConfiguration {
//   1、 @Autowired
//    MinioConfig minioConfig;
    //2、构造器注入： 创建当前组件类对象时，必须先初始化MinioConfig对象
//    public Knife4jConfiguration(MinioConfig minioConfig){
//
//    }
    @Bean
    public MinioClient minioClient() throws Exception {
        String bucket = MinioConfig.BUCKET;
        //Builder构建者设计模式
        MinioClient client = MinioClient.builder()
                //账号密码
                .credentials(MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY)
                //minio服务端 接收java客户端请求的地址： 9000 api接口地址
                .endpoint(MinioConfig.ENDPOINT)
                //构建
                .build();
        boolean exists = client.bucketExists(BucketExistsArgs.builder()
                .bucket(bucket)
                .build());
        if(!exists){
            //桶不存在，创建桶
            client.makeBucket(MakeBucketArgs.builder()
                    .bucket(bucket)
                    .build());
            String policyJson = "{\n" +
                    "    \"Version\": \"2012-10-17\",\n" +
                    "    \"Statement\": [\n" +
                    "        {\n" +
                    "            \"Effect\": \"Allow\",\n" +
                    "            \"Principal\": {\n" +
                    "                \"AWS\": [\n" +
                    "                    \"*\"\n" +
                    "                ]\n" +
                    "            },\n" +
                    "            \"Action\": [\n" +
                    "                \"s3:GetBucketLocation\",\n" +
                    "                \"s3:ListBucket\",\n" +
                    "                \"s3:ListBucketMultipartUploads\"\n" +
                    "            ],\n" +
                    "            \"Resource\": [\n" +
                    "                \"arn:aws:s3:::"+bucket+"\"\n" +
                    "            ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"Effect\": \"Allow\",\n" +
                    "            \"Principal\": {\n" +
                    "                \"AWS\": [\n" +
                    "                    \"*\"\n" +
                    "                ]\n" +
                    "            },\n" +
                    "            \"Action\": [\n" +
                    "                \"s3:AbortMultipartUpload\",\n" +
                    "                \"s3:DeleteObject\",\n" +
                    "                \"s3:GetObject\",\n" +
                    "                \"s3:ListMultipartUploadParts\",\n" +
                    "                \"s3:PutObject\"\n" +
                    "            ],\n" +
                    "            \"Resource\": [\n" +
                    "                \"arn:aws:s3:::"+bucket+"/*\"\n" +
                    "            ]\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            //给桶设置权限
            client.setBucketPolicy(SetBucketPolicyArgs.builder()
                    //桶名称
                    .bucket(bucket)
                    //权限字符串
                    .config(policyJson)
                    .build());
        }
        return client;
    }

    @Bean
    public OpenAPI customOpenAPI() {

        return new OpenAPI().info(
                new Info()
                        .title("后台管理系统API")
                        .version("1.0")
                        .description("后台管理系统API"));
    }
    
    @Bean
    public GroupedOpenApi systemAPI() {

        return GroupedOpenApi.builder().group("系统信息管理").
                pathsToMatch(
                        "/admin/system/**"
                ).
                build();
    }

    @Bean
    public GroupedOpenApi loginAPI() {

        return GroupedOpenApi.builder().group("后台登录管理").
                pathsToMatch(
                        "/admin/login/**",
                        "/admin/info"
                ).
                build();
    }
    
    @Bean
    public GroupedOpenApi apartmentAPI() {

        return GroupedOpenApi.builder().group("公寓信息管理").
                pathsToMatch(
                        "/admin/apartment/**",
                        "/admin/room/**",
                        "/admin/label/**",
                        "/admin/facility/**",
                        "/admin/fee/**",
                        "/admin/attr/**",
                        "/admin/payment/**",
                        "/admin/region/**",
                        "/admin/term/**",
                        "/admin/file/**"
                ).build();
    }
    @Bean
    public GroupedOpenApi leaseAPI() {
        return GroupedOpenApi.builder().group("租赁信息管理").
                pathsToMatch(
                        "/admin/appointment/**",
                        "/admin/agreement/**"
                ).build();
    }
    @Bean
    public GroupedOpenApi userAPI() {
        return GroupedOpenApi.builder().group("平台用户管理").
                pathsToMatch(
                        "/admin/user/**"
                ).build();
    }
}